package com.sls.web.manager.recharge.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.recharge.pojo.Operation;
import com.sls.web.manager.recharge.pojo.OperationVO;
import com.sls.web.manager.recharge.querybean.OperationQuery;
import com.sls.web.manager.recharge.service.OperationService;
import com.sls.web.manager.socket.SuperService;
import com.whalin.MemCached.MemCachedClient;

@Service("operationService")
public class OperationServiceImpl extends SuperService implements
		OperationService {
	private Gson gson = CommonUtil.gson();
	@Resource(name = "memcachedClient")
	private MemCachedClient memcachedClient;

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, String> operation() {
		String key = this.getClass().getName() + "listOperation";
		Map<String, String> map = (Map<String, String>) this.memcachedClient
				.get(key);
		if (map == null || map.isEmpty()) {
			SocketDTO dto = new SocketDTO();
			Head head = new Head();
			head.setServiceCode(Constants.FIND_LIST_OF_OPERATION);
			head.setIp("127.0.0.1");
			head.setUserId("system");
			dto = super.send(head);
			head = dto.getHead();
			if (TransmissionCode.SUCCESS.getCode().equals(
					dto.getHead().getResponseCode())) {
				List<OperationVO> list = gson.fromJson(dto.getBody(),
						new TypeToken<List<OperationVO>>() {
						}.getType());
				if (list != null && !list.isEmpty()) {
					map = new HashMap<String, String>();
					for (OperationVO o : list) {
						map.put(o.getCode(), o.getName());
					}
					memcachedClient.set(key, map);
				}
			} else {
				throw new ManagerException(dto.getHead().getResponseMessage());
			}
		}
		return map;
	}

	@Override
	public void save(Operation operation, UserInfoVO userInfoVO)
			throws Exception {
		SocketDTO dto = new SocketDTO();
		Head head = new Head();
		head.setServiceCode(Constants.INSERT_OPERATION);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		dto = super.send(head, operation);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public void delete(Operation operation, UserInfoVO userInfoVO)
			throws Exception {
		SocketDTO dto = new SocketDTO();
		Head head = new Head();
		head.setServiceCode(Constants.DELETE_OPERATION);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		dto = super.send(head, operation);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	@Override
	public void update(Operation operation, UserInfoVO userInfoVO)
			throws Exception {
		SocketDTO dto = new SocketDTO();
		Head head = new Head();
		head.setServiceCode(Constants.UPDATE_OPERATION);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		dto = send(head, operation);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public Operation findByCode(String code, UserInfoVO userInfoVO)
			throws Exception {
		SocketDTO dto = new SocketDTO();
		Head head = new Head();
		head.setServiceCode(Constants.FIND_OPERATION_BY_CODE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		Operation operation = new Operation();
		operation.setCode(code);
		dto = super.send(head, operation);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			return gson.fromJson(dto.getBody(), Operation.class);
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public Pagination<Operation> findByPage(OperationQuery oq,
			UserInfoVO userInfoVO) throws Exception {
		SocketDTO dto = new SocketDTO();
		Head head = new Head();
		head.setServiceCode(Constants.FIND_PAGE_OF_OPERATION);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		dto = super.send(head, oq);
		head = dto.getHead();
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			return gson.fromJson(dto.getBody(),
					new TypeToken<Pagination<Operation>>() {
					}.getType());
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public List<Operation> findList(UserInfoVO userInfoVO) throws Exception {
		SocketDTO dto = new SocketDTO();
		Head head = new Head();
		head.setServiceCode(Constants.FIND_LIST_OF_OPERATION);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		dto = super.send(head);
		head = dto.getHead();
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			return gson.fromJson(dto.getBody(),
					new TypeToken<List<Operation>>() {
					}.getType());
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}
}
